package resilienceManager.qmul.gemom;
import java.sql.*; 
import java.util.StringTokenizer;
import java.lang.*;

/**
 * Saving the information in the database
 * 
 * All are static method and parameters
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */


public class Datasaver {

	// the driver of mysql 
	private final static String driver= "com.mysql.jdbc.Driver";
	// the url of database
	private final static String url="jdbc:mysql://127.0.0.1:3306/test";
	// the username
	private final static String user="root";
	// the password
	private final static String password = "admin"; 


	// transfer the namespace string to the path
	public static String nstransfer(String s)
	{
		String xs;
		String splitPattern1 = "/";
		String splitPattern2 = ",";

		xs=s.replaceAll(splitPattern1, splitPattern2);

		return xs;

	}
	//transfer the path to the namesapce string
	public static String pathtransfer(String s)
	{
		String xs;
		String splitPattern1 = "/";
		String splitPattern2 = ",";

		xs=s.replaceAll(splitPattern2, splitPattern1);

		return xs;

	}

	// insert a namespace to the database 
	public static void insert(namespace nt, int tableID)
	{
		try { 
			// load the driver
			Class.forName(driver); 

			// connect with database
			Connection conn = DriverManager.getConnection(url, user, password); 

			if(!conn.isClosed()) 
			{
				//System.out.println("Succeeded connecting to the Database!");	
			}


			Statement statement = conn.createStatement();
//			namespace0 if CbrData(0)
			String table="namespace"+tableID;
			String sql="insert into "+table+" (nsid,content) values ("+nt.getID()+",'"+nt.getcontent()+"')";
			System.out.println(sql);
			statement.executeUpdate(sql); 

			conn.close(); 

		} catch(Exception e) { 

			e.printStackTrace(); 
		}
	}
	
	// get all the namespace from database
	public static namespace[] getNS(int tableID)
	{
		
		namespace[] nsset = null;
		
		try { 
			// load the driver
			Class.forName(driver); 

			// connect with database
			Connection conn = DriverManager.getConnection(url, user, password); 

			if(!conn.isClosed()) 
			{
				//System.out.println("Succeeded connecting to the Database!");	
			}


			Statement statement = conn.createStatement();
			String table="namespace"+tableID;
			String sql="select count(*) from "+table;
			
			ResultSet rs = statement.executeQuery(sql);
			
			int num=0;
            
            if(rs.next()){
            	num=rs.getInt(1);
            }
			
			nsset=new namespace[num];
			
			sql="select * from "+table;
            
            rs = statement.executeQuery(sql);
            
            int t_index=0;
            
            while(rs.next())
            {
            	 int tid=rs.getInt(1);
             	 String tc=rs.getString(2);
                 nsset[t_index]=new namespace(tid,tc);   
                            
                 t_index++;
            }
			

			conn.close(); 

		} catch(Exception e) { 

			e.printStackTrace(); 
		}
		
		return nsset;
		
	}
	
	// according to the content of namespace, get the ID
	public static int findID(String content, int tableID)
	{
		int nsID=-1;
		
		
		try { 
			// load the driver
			Class.forName(driver); 

			// connect with database
			Connection conn = DriverManager.getConnection(url, user, password); 

			if(!conn.isClosed()) 
			{
				//System.out.println("Succeeded connecting to the Database!");	
			}


			Statement statement = conn.createStatement();
			
			String table="namespace"+tableID;
			String sql="select * from "+table;
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next())
            {
            	 int tid=rs.getInt(1);
            	 
             	 String tc=rs.getString(2);
  
             	if(tc.endsWith(content))
             	{
             		nsID=tid;
             		return nsID;
             	}

             	 
            }
			
		}catch(Exception e) { 

			e.printStackTrace(); 
		}
		
		
		return nsID;
		
	}
	
	
	// save the auction item in data base
	public static void saveItems(NSitem[] items, int tableID)
	{
		try { 
			// load the driver
			Class.forName(driver); 

			// connect with database
			Connection conn = DriverManager.getConnection(url, user, password); 

			if(!conn.isClosed()) 
			{
				//System.out.println("Succeeded connecting to the Database!");	
			}


			Statement statement = conn.createStatement();
			
			String table="item"+tableID;
			
			String sql="delete from "+table;
			
			statement.execute(sql);
			
			for(int i=0; i<items.length; i++)
			{
				NSitem item_temp=items[i];
				
				String content="";
				
				for(int j=0; j<item_temp.nslist.size();j++)
				{
					content=content+item_temp.nslist.get(j)+",";
				}
				sql="insert into "+table+" (id,content) values ("+item_temp.getID()+",'"+content+"')";
				
				statement.executeUpdate(sql);
			}
			
			conn.close(); 

		} catch(Exception e) { 

			e.printStackTrace(); 
		}
	}
	
	
	// load the item from the data base
	
	public static NSitem[] getItems(int tableid){
		
		NSitem[] items=null;
		
		
		try { 
			// load the driver
			Class.forName(driver); 

			// connect with database
			Connection conn = DriverManager.getConnection(url, user, password); 

			if(!conn.isClosed()) 
			{
				//System.out.println("Succeeded connecting to the Database!");	
			}


			Statement statement = conn.createStatement();
			
			String table= "item"+tableid;
			String sql="select count(*) from "+table;
			
			ResultSet rs = statement.executeQuery(sql);
			
			int num=0;
            
            if(rs.next()){
            	num=rs.getInt(1);
            }
			
			items=new NSitem[num];
			
			sql="select * from "+table;
            
            rs = statement.executeQuery(sql);
            
            int t_index=0;
            
            while(rs.next())
            {
            	 int id=rs.getInt(1);
             	 String content=rs.getString(2);
                 
             	items[id]=new NSitem(id);
             	 String[] t=content.split(",");
             	 
             	 for(int i=0; i<t.length; i++)
             	 {
             		 int nsid=Integer.parseInt(t[i]);
             		 
             		 items[id].addNS(nsid);
             	 }
                            
                 t_index++;
            }
			

			conn.close(); 

		} catch(Exception e) { 

			e.printStackTrace(); 
		}
		
		return items;
		
		
	}
	



}
